//
//  HomeViewController.m
//  shopping
//
//  Created by 马田成 on 2017/8/30.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeFirstTableViewCell.h"
#import "lunboTableViewCell.h"
#import "HomeNoticeTableViewCell.h"
#import "HomeDownTableViewCell.h"
#import "HomeDownCollectionViewCell.h"
#import "HomeNewShopViewController.h"//新商户
#import "HomeNewLYViewController.h"//新旅游
#import "HomeNewLSSViewController.h"//新零售
#import "HomeNewJRRViewController.h"//新金融
#import "scanQRCodeViewController.h"//扫描
#import "MineMyPingJiaDDViewController.h"//收款临时
#import "HomeFuKuanShareViewController.h"
#import "nnnnnnnnnsssssssViewController.h"//新生活
#import "PingTaIHulianViewController.h"
#import "LMJScrollTextView2.h"
#import "kefuViewController.h"//客服
@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,LMJScrollTextView2Delegate>
{

    UICollectionView *downcollectionView;
    LMJScrollTextView2 * _scrollTextView;
    

}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *dictOfData;

@end

@implementation HomeViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [self creationDate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationHeaderView];
    [self creationMainView];
    [self regeditCell];
     _scrollTextView.textDataArr = [NSMutableArray array];
    
    
    // Do any additional setup after loading the view.
}
- (void)creationHeaderView{

    UIImageView *imageOfSao = [[UIImageView alloc] init];
    [self.view addSubview:imageOfSao];
    [imageOfSao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(37);
        make.left.equalTo(self.view).with.offset(53);
        make.height.offset(25);
        make.width.offset(25);
        
    }];
    imageOfSao.image = [UIImage imageNamed:@"shouye_icon_saoyisao"];
    UIButton *buttonSaoSao = [[UIButton alloc] init];
    [self.view addSubview:buttonSaoSao];
    [buttonSaoSao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(37);
        make.left.equalTo(self.view).with.offset(53);
        make.height.offset(40);
        make.width.offset(100);
        
        
    }];
    buttonSaoSao.backgroundColor = [UIColor clearColor];
    [buttonSaoSao addTarget:self action:@selector(saosaoBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *labelOfSao = [[UILabel alloc] init];
    [self.view addSubview:labelOfSao];
    [labelOfSao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageOfSao.mas_centerY);
        make.left.equalTo(imageOfSao.mas_right).with.offset(13);
        make.height.offset(18);
        make.width.offset(70);
        
    }];
    labelOfSao.text = @"扫一扫";
    labelOfSao.font = [UIFont systemFontOfSize:18];
    labelOfSao.textColor = [UIColor colorWithRed:85 / 255.0f green:142 / 255.0f blue:233 / 255.0f alpha:1];
    UIView *viewGray = [[UIView alloc] init];
    [self.view addSubview:viewGray];
    [viewGray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(27);
        make.centerX.equalTo(self.view);
        make.height.offset(46);
        make.width.offset(1);
        
    }];
    viewGray.backgroundColor = getColor(@"ECECEC");
    UIImageView *imageOfMoney = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shouye_icon_shoukuan"]];
    [self.view addSubview:imageOfMoney];
    [imageOfMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(viewGray.mas_right).with.offset(52);
        make.centerY.equalTo(imageOfSao.mas_centerY);
        make.height.offset(25);
        make.width.offset(25);
        
    }];
    UILabel *labelOfMoney = [[UILabel alloc] init];
    [self.view addSubview:labelOfMoney];
    [labelOfMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageOfSao.mas_centerY);
        make.left.equalTo(imageOfMoney.mas_right).with.offset(13);
        make.height.offset(18);
        make.width.offset(70);
        
    }];
    labelOfMoney.text = @"收款";
    labelOfMoney.font = [UIFont systemFontOfSize:18];
    labelOfMoney.textColor = [UIColor colorWithRed:85 / 255.0f green:142 / 255.0f blue:233 / 255.0f alpha:1];
    
    UIButton *buttonShouKuan = [[UIButton alloc] init];
    [self.view addSubview:buttonShouKuan];
    [buttonShouKuan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(37);
        make.right.equalTo(self.view).with.offset(-20);
        make.height.offset(40);
        make.width.offset(DEF_SCREEN_WIDTH / 2);
        
        
    }];
    buttonShouKuan.backgroundColor = [UIColor clearColor];
    [buttonShouKuan addTarget:self action:@selector(shouKuanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *viewOfLine = [[UIView alloc] initWithFrame:CGRectMake(0, 82, DEF_SCREEN_WIDTH, 1)];
    [self.view addSubview:viewOfLine];
    viewOfLine.backgroundColor = getColor(@"ECECEC");
    
}
- (void)creationMainView{
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 83, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 83 - 49);
  
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
   
    self.view.backgroundColor = [UIColor colorWithRed:245 / 255.0f green:245 / 255.0f blue:245 / 255.0f alpha:1];

    

    
}
- (void)regeditCell{

    [self.tableView registerNib:[UINib nibWithNibName:@"HomeFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeFirstTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeNoticeTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeNoticeTableViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeDownTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeDownTableViewCell"];
    
}
- (void)creationDate{
    
    _dictOfData = [[NSMutableDictionary alloc] init];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
   // NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
    
    [productParams setValue:token forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:adminUserIndexj networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            _dictOfData = [returnData objectForKey:@"data"];
            carouselArr = [_dictOfData objectForKey:@"banner"];
            arrOfGG = [_dictOfData objectForKey:@"notice"];
           
            NSLog(@"!!!!!!!!!!!!!!!%@", _dictOfData);
            NSLog(@"!!!!!!!!!!!!!!!%@", arrOfGG );
            [self.tableView reloadData];
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
}


#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 120;
    }else if (indexPath.row == 1) {
    
        return 141;
        
    }else if (indexPath.row == 2) {
        
        return 36;
        
    }else {
        
        return 450;
        
    }
    
    
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        HomeFirstTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomeFirstTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
        dic = [_dictOfData objectForKey:@"money"];
        cell.money_lvyou.text = [dic objectForKey:@"money_lvyou"];
        cell.money_jiaoyi.text = [dic objectForKey:@"money_jiaoyi"];
        cell.money_shangcheng_new.text = [dic objectForKey:@"money_shangcheng_new"];
        
        
        return cell;
    }else if (indexPath.row == 1) {
    
        lunboTableViewCell *cell=(lunboTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"lunboTableViewCell"];
        if (cell == nil) {
            cell=[[lunboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"lunboTableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if (carouselArr) {
            
            [cell updateInfo:carouselArr];
            
        }
        cell.secondViewController=self;
        return cell;

    }else if (indexPath.row == 2) {
        
        HomeNoticeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomeNoticeTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.leftLab.text = [arrOfGG[0] objectForKey:@"articleTitle"];
        
        
        
        // [_scrollTextView startScrollBottomToTop];
//        _scrollTextView = [[LMJScrollTextView2 alloc] initWithFrame:CGRectMake(10, 10, DEF_SCREEN_WIDTH, 20)];
//        _scrollTextView.delegate        = self;
//        _scrollTextView.textColor       = [UIColor blackColor];
//        _scrollTextView.textFont        = [UIFont systemFontOfSize:12.f];
//     
//       
//        
//        //    [_scrollTextView.textDataArr addObjectsFromArray:myarr];
//        
//        
//        for (int i=0; i<arrOfGG.count; i++) {
//            
//            NSString *str=[arrOfGG[i] objectForKey:@"articleTitle"];
//            
//            [_scrollTextView.textDataArr addObject:str];
//        }
//      //  _scrollTextView.textDataArr     = arrOfGG;
//        
//        NSLog(@"%@", _scrollTextView.textDataArr);
//        [cell addSubview:_scrollTextView];
//        [_scrollTextView startScrollBottomToTop];
//        
        
        
//        if (arrOfGG) {
//            
//            [cell updateInfo:arrOfGG];
//            
//        }
//        cell.arr = arrOfGG;
        
        return cell;
        
        
    } else{
        
        HomeDownTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"HomeDownTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.itemSize = CGSizeMake((DEF_SCREEN_WIDTH - 40) / 3, 130);
        
        downcollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 450) collectionViewLayout:layout];
        //
        [downcollectionView layoutIfNeeded];
        //    self.automaticallyAdjustsScrollViewInsets = NO;
        downcollectionView.pagingEnabled = NO;
        downcollectionView.delegate = self;
        downcollectionView.dataSource = self;
        
        
        [downcollectionView registerNib:[UINib nibWithNibName:@"HomeDownCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeDownCollectionViewCell"];
        
        downcollectionView.showsHorizontalScrollIndicator = NO;
        downcollectionView.showsVerticalScrollIndicator = NO;
        
        
        //    self.collectionView.bounces = NO;
        downcollectionView.backgroundColor = [UIColor whiteColor];
        
        [cell addSubview:downcollectionView];

        

        return cell;
        
    }
    
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//    HomeNewShopViewController *vc = [[HomeNewShopViewController alloc] init];
//    vc.hidesBottomBarWhenPushed = YES;
//    
//    [self.navigationController pushViewController:vc animated:YES];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}
//数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 8;

  //  return 6;
    
}
//内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeDownCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeDownCollectionViewCell" forIndexPath:indexPath];
//    NSArray *arrOfTitle = @[@"新零售", @"新游戏", @"新旅游", @"新公益", @"新商户",@"新生活", @"平台互联"];
    
    NSArray *arrOfTitle = @[@"新零售", @"新旅游", @"新商户",@"新生活", @"平台互联",@"新游戏",@"新公益",@"客服"];
    cell.titleLabel.text = arrOfTitle[indexPath.row];
    
    NSArray *arrOfPic = @[@"lingshou", @"lvyou", @"shangcheng", @"shenghuo",@"lianjie", @"youxi",@"gongyiaa",@"keuf"];
//    NSArray *arrOfPic = @[@"home_icon_lingshou", @"home_icon_youxi", @"home_icon_lvyou", @"home_icon_jinrong", @"home_icon_shanghu", @"new_life",@"home_icon_hulian"];
    cell.imageView.image = [UIImage imageNamed:arrOfPic[indexPath.row]];

    return cell;
    
    
    
}
//点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

//    if (indexPath.row == 4) {
//        HomeNewShopViewController *vc = [[HomeNewShopViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
//        
//        
//    }else if (indexPath.row == 2) {
//    
//        HomeNewLYViewController *vc = [[HomeNewLYViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
//        
//    
//    }else if (indexPath.row == 0) {
//        
//        HomeNewLSSViewController *vc = [[HomeNewLSSViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
//        
//        
//    }else if (indexPath.row == 3){
//    
//        HomeNewJRRViewController *vc = [[HomeNewJRRViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
//        
//    
//    }
    
    if (indexPath.row == 2) {
//        HomeNewShopViewController *vc = [[HomeNewShopViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
        
        [PublicFunction showAlert:nil message:@"开发中，敬请期待..."duration:5.0f];
        
    }else if (indexPath.row == 1) {
        
        HomeNewLYViewController *vc = [[HomeNewLYViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if (indexPath.row == 0) {
        
        HomeNewLSSViewController *vc = [[HomeNewLSSViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
    else if (indexPath.row == 3){
        
//        nnnnnnnnnsssssssViewController *vc = [[nnnnnnnnnsssssssViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
        
        [PublicFunction showAlert:nil message:@"开发中，敬请期待..."duration:5.0f];
        
    }else if (indexPath.row == 4){
        
        
        PingTaIHulianViewController *vc = [[PingTaIHulianViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
//        NSString *username = @"gslm";
//        [self openUserPage:username];
        
        
    }else if (indexPath.row == 5){
        
        
       [PublicFunction showAlert:nil message:@"开发中，敬请期待..."duration:5.0f];
        
        
    }else if (indexPath.row == 6){
        
        
        [PublicFunction showAlert:nil message:@"开发中，敬请期待..."duration:5.0f];
        
        
    }else if (indexPath.row == 7){
    
        kefuViewController *vc = [[kefuViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    
    }

    
    
    
}

- (BOOL)isInstagramInstalled {
    NSURL *instagramURL = [NSURL URLWithString:@"gslm://location?id=1"];
    return [[UIApplication sharedApplication] canOpenURL:instagramURL];
}

- (void)openUserPage:(NSString *)username {
    NSURL *fansPageURL;
    if ([self isInstagramInstalled]) {
        fansPageURL = [NSURL URLWithString:[NSString stringWithFormat:@"gslm://user?username=%@", username]];
    } else {
        fansPageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://instagram.com/%@", username]];
    }
    [[UIApplication sharedApplication] openURL:fansPageURL];
}

//扫一扫
- (void)saosaoBtnClick{

    scanQRCodeViewController *vc = [[scanQRCodeViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
//收款/临时支付界面
- (void)shouKuanBtnClick{

    HomeFuKuanShareViewController *vc = [[HomeFuKuanShareViewController alloc] init];
//    vc.goodsId = goodsId;
//    vc.shopId = shopId;
//    vc.orderId = orderId;
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
//
}

- (void)scrollTextView2:(LMJScrollTextView2 *)scrollTextView currentTextIndex:(NSInteger)index{
    NSLog(@"当前是信息%ld",index);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
